import { token } from "@/api/index";
import { removeToken } from "@/utils/storage/token";
export const useApp = defineStore(
  "app",
  () => {
    const appTitle = ref(import.meta.env.VITE_APP_TITLE),
      // 是否缩小
      collapse = ref(false), // 导航栏是否收起
      windowHeight = ref(0), // 窗口高度
      hasLogin = ref(false), // 是否登陆
      userInfo: any = ref({}); // 员工信息
    // 后台判断登录状态
    const checkLogin = async () => {
      const res: any = await token();
      if (res.success) {
        hasLogin.value = true;
      }
    };
    // 登录
    const loginIn = (val: any) => {
      hasLogin.value = true;
      userInfo.value = val;
    };
    // 退出登录
    const loginOut = () => {
      removeToken();
      userInfo.value = {};
    };
    // 设置可视化见面高度
    const setHeight = (val: number) => {
      windowHeight.value = val;
    };
    // 导航了缩放
    const toggleCollapse = (val: boolean) => {
      collapse.value = val;
    };
    return {
      appTitle,
      windowHeight,
      collapse,
      userInfo,
      hasLogin,
      loginIn,
      loginOut,
      checkLogin,
      setHeight,
      toggleCollapse,
    };
  },
  {
    persist: {
      key: "institutionAppKey",
      storage: localStorage,
    },
  }
);
